package com.zhouwd.zcamera;

import android.app.Activity;
import android.content.Intent;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.zhouwd.zcamera.entity.StartRecordParam;

@Route(path = "/z_media/z_camera_activity")
public class ZCameraActivity extends AppCompatActivity{
    private static final String TAG = ZCameraActivity.class.getSimpleName();

    Button activityZcameraStart;
    private GLSurfaceView activityZcameraGlsv;
    private PreviewRender previewRender;
    private boolean isStart = false;

    private AudioRecordManager audioRecordManager;
    //private MediaRecordManager mediaRecordManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //getSystemService(Context.WINDOW_SERVICE);

        setContentView(R.layout.activity_zcamera);

        initMediaRecord();

        initAudio();

        initGLSurfaceView();

        initListener();
    }

    private void initMediaRecord() {
        //mediaRecordManager = new MediaRecordManager();
    }


    private void initAudio() {
        audioRecordManager = new AudioRecordManager();
        audioRecordManager.addDataCallBack(dataCallBack);
    }

    private AudioRecordManager.DataCallBack dataCallBack = new AudioRecordManager.DataCallBack() {
        @Override
        public void callBack(byte[] audiodata) {

            JniUtil.sendAudioData(audiodata);
            /*try {
                mediaRecordManager.encodeAudio(audiodata);
            } catch (Exception e) {
                e.printStackTrace();
            }*/
        }
    };

    /**
     * 初始化监听器
     */
    private void initListener() {
        activityZcameraStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startClick();
            }
        });
    }

    /**
     * 开始点击
     */
    private void startClick() {
        if (isStart) {
            isStart = false;
            //结束录制
            activityZcameraStart.setText("开始");
            audioRecordManager.stopRecord();
            JniUtil.stopRecord();
            //mediaRecordManager.stop();
        } else {
            isStart = true;
            //开始录制
            activityZcameraStart.setText("暂停");

            StartRecordParam startRecordParam = new StartRecordParam();
            audioRecordManager.startRecord();
            //previewRender.startEncode();
            JniUtil.startRecord(startRecordParam);
            //mediaRecordManager.start();
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        activityZcameraGlsv.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        activityZcameraGlsv.onPause();
    }


    /**
     * 初始化渲染界面
     */
    private void initGLSurfaceView() {
        Log.i(TAG, "initGLSurfaceView: ");
        activityZcameraGlsv = findViewById(R.id.activity_zcamera_glsv);
        activityZcameraStart = findViewById(R.id.activity_zcamera_start);
        activityZcameraGlsv.setEGLContextClientVersion(3);

        previewRender = new PreviewRender(this, activityZcameraGlsv);
        activityZcameraGlsv.setRenderer(previewRender);
        /*渲染方式，RENDERMODE_WHEN_DIRTY表示被动渲染，只有在调用requestRender或者onResume等方法时才会进行渲染。RENDERMODE_CONTINUOUSLY表示持续渲染*/
        activityZcameraGlsv.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        previewRender.destroy();
        //mediaRecordManager.release();
    }


}
